
package com.interview.case04;

import java.math.BigDecimal;

/**
 * ThresholdDiscountPromotion类，实现满减促销策略。
 */
public class ThresholdDiscountPromotion implements Promotion {
    private BigDecimal threshold; // 阈值，例如100元
    private BigDecimal discountAmount; // 减免金额，例如10元

    /**
     * 构造方法。
     *
     * @param threshold      阈值（元）
     * @param discountAmount 减免金额（元）
     */
    public ThresholdDiscountPromotion(BigDecimal threshold, BigDecimal discountAmount) {
        if (threshold.compareTo(BigDecimal.ZERO) <= 0) {
            throw new IllegalArgumentException("阈值必须大于0");
        }
        if (discountAmount.compareTo(BigDecimal.ZERO) <= 0) {
            throw new IllegalArgumentException("减免金额必须大于0");
        }
        this.threshold = threshold;
        this.discountAmount = discountAmount;
    }

    @Override
    public BigDecimal apply(BigDecimal total) {
        if (total.compareTo(threshold) >= 0) {
            return total.subtract(discountAmount);
        }
        return total;
    }
}
